package com.paomi.client.fragment.show.presenter;

import android.app.Activity;
import android.content.Context;

import com.paomi.client.R;
import com.paomi.client.base.BaseLoadMorePresenter;
import com.paomi.client.base.BaseMvpLCEView;
import com.paomi.client.fragment.first.model.TicketJson;
import com.paomi.client.fragment.first.model.TicketListJson;
import com.paomi.client.net.Api;
import com.paomi.client.net.BaseCallback;
import com.paomi.client.net.Constants;
import com.paomi.client.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * Created by pc on 2018/10/25
 * Describe:演出数据
 * com.paomi.client.fragment.first.presenter
 */
public class ShowFraPre extends BaseLoadMorePresenter<BaseMvpLCEView> {

    private List<TicketJson> ticketJsonList=new ArrayList<>();
    private int page1=1;
    private String content;
    private String type;
    private Activity activity;
    public void setShowData(Activity activity, int page, String content, String type){
        page1=page;
        this.activity=activity;
        this.content=content;
        this.type=type;
        loadData(true);
    }
    @Override
    protected void loadData(int page, int skip, boolean pullToRefresh) {
        if(content!=null){
            Map<String,String> stringMap=new HashMap<>();
            stringMap.put("content",content);
            stringMap.put("pageNum",""+page1);
            stringMap.put("pageSize",""+Constants.PAGE_SIZE);
            stringMap.put("sort",type);
            Api.getInstanceService(Constants.Ticket_URL).getSearchTicket(stringMap).enqueue(new BaseCallback<TicketListJson>() {
                @Override
                public void onResponse(Call<TicketListJson> call, TicketListJson result) {
                    if(page1==1){
                        ticketJsonList.clear();
                    }
                    if(ticketJsonList.size()>0){
                        //判断是否有重复数据
                        boolean isData=false;
                        List<TicketJson>  comments1=result.getReturnData().getData();
                        for(TicketJson comment:comments1){
                            for(int i=0;i<ticketJsonList.size();i++){
                                if(comment.getId() == ticketJsonList.get(i).getId()){
                                    isData=false;
                                    break;
                                }else{
                                    isData=true;
                                }
                            }
                        }
                        if(isData){
                            ticketJsonList.addAll(result.getReturnData().getData());
                        }else{
                            ToastUtil.getToastEmail().ToastShow(activity, null, 1, R.layout.toast_view, activity.getResources().getString(R.string.data_null));
                        }
                    }else{
                        ticketJsonList.addAll(result.getReturnData().getData());
                    }
                    if(isViewAttached()){
                        getView().setData(ticketJsonList, true);
                    }
                }
            });
        }else{
            getView().setData(ticketJsonList, true);
        }

    }
}